<?php
/**
 * Created by PhpStorm.
 * User: lance
 * Date: 11/28/17
 * Time: 9:18 AM
 */

namespace app\customer\admin;

/**
 * 用户贷款
 */

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

class Loan extends Admin
{
    /**
     * 客户贷款资料列表
     */
    function loanList()
    {
        $a_where = [

        ];
        //客户经历只能看自己客户的贷款记录
        defined('MANAGER_ID') && ($a_where['a.manager_id'] = MANAGER_ID);
        $a_map = $this->getMap();
        foreach ($a_map as $s_key => $a_value):
            switch ($s_key):
                case 'name':
                    $a_map['a.name'] = $a_value;
                    unset($a_map[$s_key]);
                    break;
                case 'number':
                    $a_map['b.number'] = $a_value;
                    unset($a_map[$s_key]);
                    break;
            endswitch;
        endforeach;
        $a_where = array_merge($a_where, $a_map);
        $o_data = Db::name('customer a')
            ->join('customer_loan b', 'b.customer_id=a.id')
            ->where($a_where)
            ->order('b.id desc')
            ->paginate();
        //贷款类型
        $a_type = Db::name('customer_loan_type')->where('status',1)->column('id,name');
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns([
                ['id', 'ID'],
                ['name', '客户名'],
                ['number', '借据号', 'text.edit'],
                ['money', '贷款金额/元', 'number'],
                ['time', '贷款日期', 'datetime.edit'],
                ['loan_time', '贷款时长/月', 'number'],
                ['type_id','贷款类型','select',$a_type],
                ['right_button', '操作', 'btn']
            ])
            ->addFilterList('type_id',$a_type,'','radio')
            ->addValidate('CustomerLoan', 'number,money,time,loan_time')
            ->addTimeFilter('mycustomer_loan.time','','贷款开始时间,结束时间')
            ->setSearch([
                'name' => '客户名',
                'number' => '借据号'
            ])
            ->addRightButton('custom', [
                'title' => '查看贷款提醒时间',
                'icon' => 'fa fa-fw fa-eye',
                'href' => url('loanTime', [
                    'loan_id' => '__id__'
                ])
            ])
            ->addRightButton('custom', [
                'title' => '添加贷款提醒时间',
                'icon' => 'fa fa-fw fa-clock-o',
                'href' => url('addLoanTime', [
                    'loan_id' => '__id__',
                    'customer_id' => '__customer_id__'
                ])
            ])
            ->setRowList($o_data)
            ->fetch();
    }

    /**
     * 添加贷款项目
     * @return mixed
     */
    public function addLoanItem()
    {
        if (request()->isAjax()):
            $o_validate = validate('CustomerLoan');
            $a_data = input('');
            if ($o_validate->check(input('')) === false):
                $this->error($o_validate->getError());
            endif;
            $o_model = model('CustomerLoan');
            if ($o_model->save($a_data)):
                $this->success("添加成功！");
            else:
                $this->error("添加失败！");
            endif;
        else:
            return ZBuilder::make('form')
                ->addHidden('customer_id', input('id/d'))
                ->addFormItems(config('loan_info'))
                ->submitConfirm()
                ->fetch();
        endif;
    }

    /**
     *添加贷款提醒时间
     */
    function addLoanTime()
    {
        if (request()->isAjax()):
            //验证
            $o_validate = validate('Loantime');
            if ($o_validate->check(input('')) === false):
                $this->error($o_validate->getError());
            endif;
            //插入数据
            $o_model = model('CustomerLoantime');
            if (!$o_model->save(input(''))):
                $this->error("操作失败！");
            endif;
            $this->success("操作成功！");
        else:
            return ZBuilder::make('form')
                ->addFormItems([
                    ['date', 'time', '贷款提醒时间',]
                ])
                ->fetch();
        endif;
    }

    /**
     * 贷款时间列表
     */
    public function loanTime()
    {
        $a_where = [];

        if (input('?loan_id')):
            $a_where['loan_id'] = (int)input('loan_id');
        endif;
        //客户只能查看自己的客户
        defined('MANAGER_ID') && ($a_where['b.manager_id'] = MANAGER_ID);
        $a_where += $this->getMap();
        $o_dataList = Db::name('customer_loantime a')
            ->field(['a.id', 'a.loan_id', 'a.time', 'a.customer_id', 'b.name customer_name'])
            ->join('customer b', 'a.customer_id=b.id')
            ->order('id desc')
            ->where($a_where)
            ->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns([
                ['id', 'ID'],
                ['loan_id', '贷款ID'],
                ['customer_id', '客户ID'],
                ['customer_name', '客户名'],
                ['time', '提醒时间', 'date.edit'],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter([
                'loan_id'
            ])
            ->addRightButton('delete')
            ->setSearch([
                'b.name' => '客户名'
            ])
            ->addTimeFilter('time')
            ->setRowList($o_dataList)
            ->setTableName('customer_loantime')
            ->fetch();
    }
}